package com.shasselclub.project.ask.datiRecord.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.shasselclub.api.v1.result.PaiHangBang;
import com.shasselclub.api.v1.result.PaiHangBangExportBo;
import com.shasselclub.common.constant.ResultStatus;
import com.shasselclub.common.utils.DateUtils;
import com.shasselclub.common.utils.IpUtils;
import com.shasselclub.common.utils.bigdecamil.DecimalUtils;
import com.shasselclub.common.utils.map.GetParamToMap;
import com.shasselclub.common.utils.string.StringUtil;
import com.shasselclub.project.ask.question.domain.Question;
import com.shasselclub.project.ask.question.service.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shasselclub.project.ask.datiRecord.mapper.DatiRecordMapper;
import com.shasselclub.project.ask.datiRecord.domain.DatiRecord;
import com.shasselclub.project.ask.datiRecord.service.IDatiRecordService;
import com.shasselclub.common.utils.text.Convert;

/**
 * 答题记录Service业务层处理
 * 
 * @author zhengyunfei
 * @date 2022-08-08
 */
@Service
public class DatiRecordServiceImpl implements IDatiRecordService 
{
    @Autowired
    private DatiRecordMapper datiRecordMapper;
    @Autowired
    private IQuestionService questionService;
    @Autowired
    private IDatiRecordService datiRecordService;
    /**
     * 查询答题记录
     * 
     * @param id 答题记录主键
     * @return 答题记录
     */
    @Override
    public DatiRecord selectDatiRecordById(Long id)
    {
        return datiRecordMapper.selectDatiRecordById(id);
    }

    /**
     * 查询答题记录列表
     * @return 答题记录
     */
    @Override
    public List<DatiRecord> selectDatiRecordList(Map<String,Object> queryMap)
    {
        return datiRecordMapper.selectDatiRecordList(queryMap);
    }

    /**
     * 新增答题记录
     * @return 结果
     */
    @Override
    public int insertDatiRecord(Map<String,Object> queryMap)
    {
        queryMap.put("createTime",DateUtils.getNowDate());
        return datiRecordMapper.insertDatiRecord(queryMap);
    }

    /**
     * 修改答题记录
     * @return 结果
     */
    @Override
    public int updateDatiRecord(Map<String,Object> queryMap)
    {
        queryMap.put("updateTime",DateUtils.getNowDate());
        return datiRecordMapper.updateDatiRecord(queryMap);
    }

    /**
     * 批量删除答题记录
     * 
     * @param ids 需要删除的答题记录主键
     * @return 结果
     */
    @Override
    public int deleteDatiRecordByIds(String ids)
    {
        return datiRecordMapper.deleteDatiRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除答题记录信息
     * 
     * @param id 答题记录主键
     * @return 结果
     */
    @Override
    public int deleteDatiRecordById(Long id)
    {
        return datiRecordMapper.deleteDatiRecordById(id);
    }

    @Override
    public String getMaxId(Map<String, Object> queryMap) {
        return datiRecordMapper.getMaxId(queryMap);
    }

    @Override
    public int count(Map<String, Object> parameterMap) {
        return datiRecordMapper.count(parameterMap);
    }

    @Override
    public int getSumScore(Map<String, Object> scoreQueryMap) {
        return datiRecordMapper.getSumScore(scoreQueryMap);
    }
    /**
     * 获取答题id
     * @param openId
     * @return
     */
    @Override
    public String getDaTiId(String openId,String hdId) {

        String dateTimeNow=DateUtils.dateTimeNow();//yyyyMMddHHmmss
        int random=RandomUtil.getRandom(true).nextInt(100);
        DecimalFormat decimalFormat=new DecimalFormat("00");
        dateTimeNow=dateTimeNow+decimalFormat.format(random);
        //根据openid获取答题次数
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("openId", openId);
        queryMap.put("questionId", hdId);
        String maxId=getMaxId(queryMap);
        String daTiId=maxId;
        if(StringUtil.isNullOrEmpty(maxId)||"0".equals(daTiId)){
            daTiId=dateTimeNow;
        }else{
           BigDecimal bigDecimal=new BigDecimal(daTiId);
           BigDecimal one=new BigDecimal("1");
            daTiId=DecimalUtils.add(bigDecimal, one).toString();
        }
        return daTiId;
    }

    @Override
    public List<PaiHangBang> getPangHangPang(Map<String, Object> hdQueryMap) {
        return datiRecordMapper.getPangHangPang(hdQueryMap);
    }

    @Override
    public PaiHangBang getRankNumAndSumScore(Map<String, Object> hdQueryMap) {
        return datiRecordMapper.getRankNumAndSumScore(hdQueryMap);
    }
    @Override
    public PaiHangBang getMyPaiHang(Map<String, Object> hdQueryMap) {
        return datiRecordMapper.getMyPaiHang(hdQueryMap);
    }

    @Override
    public int getAnswerNum(Map<String, Object> scoreQueryMap) {
        return datiRecordMapper.getAnswerNum(scoreQueryMap);
    }

    @Override
    public int getZhuLiNum(Map<String, Object> hdQueryMap) {
        return datiRecordMapper.getZhuLiNum(hdQueryMap);
    }

    @Override
    public DatiRecord getMaxScroreRecord(Map<String, Object> parameterMap) {
        return datiRecordMapper.getMaxScroreRecord(parameterMap);
    }

    @Override
    public int updateMaxScoreRecord(Map<String, Object> updateMaxScoreRecordMap) {
        return datiRecordMapper.updateMaxScoreRecord(updateMaxScoreRecordMap);
    }

    @Override
    public int updateNotMaxScoreRecord(Map<String, Object> updateNotMaxScoreRecordMap) {
        return datiRecordMapper.updateNotMaxScoreRecord(updateNotMaxScoreRecordMap);
    }

    @Override
    public int getLastScore(Map<String, Object> parameterMap) {
        return datiRecordMapper.getLastScore(parameterMap);
    }

    @Override
    public int getRankNum(Map<String, Object> scoreQueryMap) {
        return datiRecordMapper.getRankNum(scoreQueryMap);
    }

    /**
     * 获取最新一起的排行榜数据信息
     * @return
     */
    public List<PaiHangBangExportBo> getLastPaiHangList(){
        //问卷id=开始日期+结束日期
        String questionId = "";
        //查询问卷信息
        Question question= questionService.selectQuestion();
        if(!StringUtil.isNullOrEmpty(question)){
            Date startTime=question.getStartTime();
            Date endTime=question.getEndTime();
            questionId= DateUtil.format(startTime,DateUtils.YYYYMMDDHHMMSS)+DateUtil.format(endTime,DateUtils.YYYYMMDDHHMMSS);
        }
        Map<String,Object> hdQueryMap=new HashMap<>();
        hdQueryMap.put("questionId", questionId);
        //排行榜
        List<PaiHangBangExportBo> paiHangBangList=datiRecordMapper.getLastPaiHangList(hdQueryMap);
        return paiHangBangList;
}
}